﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Objects;

namespace AbingtonBooks.Pages.Books.ViewBook
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int? bookId = Convert.ToInt32(Request.QueryString["id"]);
            if (bookId != null)
            {
                using (dbAbingtonBooks context = new dbAbingtonBooks())
                {
                    var result = context.getBookByBookId(bookId).FirstOrDefault();
                    if (result != null)
                    {

                        //Displays Book Picture in the Image1
                        Image1.ImageUrl = result.Src;

                        //Displays Book Author in the lblAuthor text
                        lblAuthor.Text = result.Author;

                        //Displays Book Title in the lblTitle text
                        lblTitle.Text = result.Title;

                        //Displays Book Price in the lblPrice text
                        string BookPrice;
                        BookPrice = result.price.ToString();
                        lblPrice.Text = "$" + BookPrice;

                        //Displays Book ISBN in the lblISBN text
                        string BookISBN;
                        BookISBN = result.ISBN.ToString();
                        lblISBN.Text = BookISBN;

                        lblBookSummary.InnerText = result.Summary.ToString();

                        addToCart.CommandArgument = result.BookId.ToString();


                    }
                    else {
                        Response.Redirect("~/Pages/Books/BookNotFound");
                    
                    }
                }
            }
            else
            {
                Response.Redirect("~/Pages/Books");
            }

            

        }

        protected void addToCart_Command(object sender, CommandEventArgs e)
        {
            if (Session["Customer"] != null)
            {
                ShoppingCart cart = ShoppingCart.getShoppingCart();
                int bookId = Convert.ToInt32(e.CommandArgument);
                using (dbAbingtonBooks context = new dbAbingtonBooks())
                {
                    Book book = context.Books.Where(b => b.BookId == bookId).FirstOrDefault();
                    cart.addToCart(book);
                    Response.Redirect("~/Pages/Cart");
                }
            }
            else {
                Session["ToGo"] = Request.Url;
                Response.Redirect("~/Pages/Login");
            }
        }


    }
}